


var userid = false;

function userFormValidation() {

	if (userform.form()) {

		var objectToSend = new Array();

		$(".user-data-form").each(function() {

			objectToSend.push(Array(this.name, this.value));
		});

		$.ajax({
			dataType: "json",
			type: "POST",
			url: "../phpFiles/index.php",
			data: {type: "insert", user: objectToSend}
		}).done(function(msg) {

			if (msg)
			{

				var fullName = "", name = "", sex = "", conductorid = "", fail = false;
				$.each(msg, function(key, val) {

					if (key === "name") {

						name = val;
					}
					else if (key === 'last_name') {

						fullName += name + " " + val;
					}
					else if (key === 'sex') {

						sex = val;
					}
					else if (key === 'email') {

						$("#userEmail").val(val);
					}
					else if (key === 'conductorid') {

						conductorid = val;
					}
					else if (key === 'fail') {

						fail = true;
					}
					else {
						userid = val;
					}
				});
				if (!fail) {
					$("#div-loguin-form").replaceWith('<label style="float: right; text-align: right; margin-right: 30px;color: #005599; font-size: 1.2em; z-index: 999">Bievenue ' + sex + ' ' + name + '<a href="" onmousedown="logout()"> déconnexion</a></label>');
					$("<option value='" + conductorid + "' selected>" + sex + ' ' + name + "</option>").appendTo("#conductor_name");
					usertweencar.play();
				}
				else {
					alert("Oups .. un problème, s'il vous plaît réessayer plus tard. Merci.");
				}
			}

		});

	}
}

function reservationFormValidation() {

	if (carForm.form() && reservationForm.form()) {

		reservation = new Array();
		car = new Array();
		conductor = $(".conductor-select-form").val();
		var dataConfirm = new reservationData();

		$.ajax({
			beforeSend: function() {
				$('#okR').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
			},
			complete: function() {
				$('#loading').replaceWith('<div id="okR" class="button_okR" style="cursor: pointer; float: none" onclick="reservationFormValidation()"><label style=" cursor: inherit;">OK</label></div>');
			},
			dataType: "json",
			async: false,
			type: "POST",
			url: "../phpFiles/index.php",
			data: {type: "get", conductor: conductor}
		}).done(function(msg) {

			if (msg)
			{

				$.each(msg, function(key, val) {

					dataConfirm.reservation[ key ] = val;
				});
			}

		});
		if ($('#carSelect').length > 0) {

			car = $('#carSelect').val();
			$.ajax({
				dataType: "json",
				async: false,
				type: "POST",
				url: "../phpFiles/index.php",
				data: {type: "get", car: car}
			}).done(function(msg) {

				if (msg)
				{

					$.each(msg, function(key, val) {

						dataConfirm.reservation[ key ] = val;
					});
				}

			});
		}
		else {

			$(".car-data-form").each(function() {

				if (this.name !== 'carSelect') {

					car.push(Array(this.name, this.value));
					dataConfirm.reservation[ this.name ] = this.value;
				}

			});
		}

		$(".reservation-data-form").each(function() {

			reservation.push(Array(this.name, this.value));
			if (this.name === 'conductor')
				dataConfirm.reservation[ this.name ] = dataConfirm.reservation[ 'sex' ] + '. ' + this.value;
			else
				dataConfirm.reservation[ this.name ] = this.value;
		});
		reservation.push(Array('baggage', $("[name='baggage']:checked").val()));
		dataConfirm.reservation[ 'baggage' ] = $("[name='baggage']:checked").val();
		dataConfirm.reservation[ 'emailConfirm' ] = $("#userEmail").val();
		var charge = getCharge(dataConfirm.reservation[ 'day_date_car_in' ], dataConfirm.reservation[ 'day_date_car_out' ]);
		if (charge) {

			var serviceCharge = 0;
			if ((typeof $("input[name='washing']:checked").val() !== "undefined")) {

				reservation.push(Array("washing", $("input[name='washing']:checked").attr('id')));
				serviceCharge += parseInt($("input[name='washing']:checked").val());
				if ($("#service1").length)
					$("#service1").replaceWith('<tr id="service1"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Lavage a la main: extérieur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='washing']:checked").val() + ',00</strong></label></td></tr>');
				else
					$('<tr id="service1"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Lavage a la main: extérieur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='washing']:checked").val() + ',00</strong></label></td></tr>').insertBefore("#serviceMark");
			}
			else {
				if ($("#service1").length)
					$("#service1").replaceWith('');
			}
			if ((typeof $("input[name='aspirate']:checked").val() !== "undefined")) {

				reservation.push(Array("aspirate", $("input[name='aspirate']:checked").attr('id')));
				serviceCharge += parseInt($("input[name='aspirate']:checked").val());
				if ($("#service2").length)
					$("#service2").replaceWith('<tr id="service2"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Aspirateur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='aspirate']:checked").val() + ',00</strong></label></td></tr>');
				else
					$('<tr id="service2"><td><label class="displayContentConfirm" style="width: 10px" >1</label></td><td style="width: 200px"><label class="displayContentConfirm" style="width: 300px;" >Aspirateur</label></td><td></td><td><label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + $("input[name='aspirate']:checked").val() + ',00</strong></label></td></tr>').insertBefore("#serviceMark");
			}
			else {
				if ($("#service2").length)
					$("#service2").replaceWith('');
			}

			var total = parseInt(charge.charge + serviceCharge), tarifV = parseFloat(charge.charge - 25);
			reservation.push(Array('charge', total, 'days_car_in', charge.days));
			reservation.push(Array('days_car_in', charge.days));
			dataConfirm.reservation[ 'charge' ] = '<label class="displayContentConfirm">CHF</label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right">' + tarifV + ',00</label>';
			dataConfirm.reservation[ 'days' ] = charge.days;
			dataConfirm.reservation[ 'total' ] = '<label class="displayContentConfirm"><strong>CHF</strong></label><label class="displayContentConfirm" style="width: 40px; text-align: right; margin-right: 10px; float: right"><strong>' + total + ',00</strong></label>';
			$(".confirm-data-reservation").each(function() {

				if (this.id === "day_date_car_in") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_car_in' ] + "</strong>");
				}
				else if (this.id === "date_fly") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_fly' ] + "</strong>");
				}
				else if (this.id === "day_date_car_out") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_car_out' ] + "</strong>");
				}
				else if (this.id === "date_fly_back") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + " " + dataConfirm.reservation[ 'hour_fly_back' ] + "</strong>");
				}
				else if (this.id === "phone_movil2") {

					$(this).html("Mobile: <strong>" + dataConfirm.reservation[ "phone_movil" ] + "</strong>");
				}
				else if (this.id === "emailConfirm") {

					$(this).html("Email: <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}
				else if (this.id === "days") {

					$(this).html(dataConfirm.reservation[ this.id ]);
				}
				else if (this.id === "sex") {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + ".</strong>");
				}
				else if (this.id === "address") {

					$(this).html("Adresses (Rue et No): <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}
				else if (this.id === "postal_code") {

					$(this).html("C.P/localité: <strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}
				else {

					$(this).html("<strong>" + dataConfirm.reservation[ this.id ] + "</strong>");
				}


			});
			reservationtweenConfirm.play();
			TweenLite.to(window, 3, {scrollTo: {y: 0, x: 0}, ease: Power2.easeOut, delay: 0.3});
		}
		else {

			alert("Les dates sont incorrects.");
		}

	}
}

function showConductorForm() {

	$("#conductor-form").each(function() {
//this.reset();
	});
	//$("#div-conductor").css("border", "solid 4px #e71e26");
	TweenMax.to("#div-conductor", 1, {css: {height: 367}});
}

function showCarForm() {

	$("#add-car-form").each(function() {
//this.reset();
	});
	//$("#div-car").css("border", "solid 4px #e71e26");
	TweenMax.to("#div-car", 1, {css: {height: 245}});

}

function addConductor() {

	if (conductorForm.form()) {

		var conductor = new Array();
		$(".conductor-data-form").each(function() {

			conductor.push(Array(this.name, this.value));
		});
		$.ajax({
			dataType: "json",
			type: "POST",
			url: "../phpFiles/index.php",
			data: {type: 'insert', conductor: conductor}
		}).done(function(msg) {

			if (msg)
			{
				var fullName = "", conductorid = "", fail = false;
				$.each(msg, function(key, val) {

					if (key === "name") {

						fullName = val;
					}
					else if (key === "last_name") {

						fullName += " " + val;
					}
					else if (key === "conductorid") {

						conductorid = val;
					}
					else if (key === "fail") {

						alert("Sorry, there are some error. Please try later.")
					}
				});
				if (!fail) {

					$("<option value='" + conductorid + "' selected>" + fullName + "</option>").appendTo("#conductor_name");
					TweenMax.to("#div-conductor", 1, {css: {height: 0}});
					return true;
				}


			}

		});
	}
	return false;
}

function addCar() {

	if (addCarForm.form()) {

		var car = new Array();
		$(".add-car-data-form").each(function() {

			car.push(Array(this.name, this.value));
		});
		$.ajax({
			dataType: "json",
			type: "POST",
			url: "../phpFiles/index.php",
			data: {type: 'insert', car: car}
		}).done(function(msg) {

			if (msg)
			{
				var mark = "", carid = "", fail = false;
				$.each(msg, function(key, val) {

					if (key === "mark") {

						mark = val;
					}
					else if (key === "carid") {

						carid = val;
					}
					else if (key === "fail") {

						alert("Sorry, there are some error. Please try later.")
					}
				});
				if (!fail) {

					$("<option value='" + carid + "' selected>" + mark + "</option>").appendTo("#carSelect");
					TweenMax.to("#div-car", 1, {css: {height: 0}});
					return true;
				}


			}

		});
	}
	return false;
}

function sendReservation() {

	$.ajax({
		beforeSend: function() {
			$('#sendR').replaceWith('<img id="loading" style="width: 25px; height: 25px; margin-left: 10px" src="images/loading.gif"></img>');
		},
		complete: function() {
			$('#loading').replaceWith('<div id="sendR" class="button_okR " style=" cursor: pointer; margin-top: 3px;" onclick="sendReservation()"><label style="margin-left: 10px;cursor: inherit;">OK</label></div>');
		},
		type: "POST",
		url: "../phpFiles/index.php",
		data: {type: 'insert', reservation: reservation, car: car, conductor: conductor}
	}).done(function(msg) {
	
		if (msg)
		{
			reservationtweenConfirmFinal.play();
			TweenLite.to(window, 1, {scrollTo: {y: 0}});
		}

	});
}